import reqAddress from '../req-set.js'
let state = {
	goodesType: [],
	goodesList: [],
}

let mutations = {
	setGoodesType(state, v) {
		state.goodesType = v
	},
	setGoodsList(state, v) {
		state.goodesList = v
	},
	setGoodsLoadMore(state, v) {
		state.goodesList = state.goodesList.concat(v)
	}
}

let actions = {
	//获取商品类型
	getGoodesType(store, param) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: `${reqAddress}/categray`,
				method: 'GET',
				success: (resp) => {
					resp.data.cates.shift()
					store.commit('setGoodesType', resp.data.cates)
					resolve(resp.data.cates)
				},
				fail: (err) => {
					reject(err)
				}
			})
		})
	},
	//获取商品列表
	reqHomeGoodesList(store, param) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: `${reqAddress}/command`,
				method: 'GET',
				success: (resp) => {
					let mutaion = 'setGoodsList'
					goodesDataHandle(store, resp, param, mutaion)
					resolve('成功')
				},
				fail: (error) => {
					reject(error)
				}
			})
		})
	},
	//获取商品类型列表
	reqHomeGoodesTypeList(store, param) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: `http://127.0.0.1:3000/hot?material_id=${param.idx}`,
				method: "GET",
				success: (resp) => {
					let mutaion = 'setGoodsList'
					goodesDataHandle(store, resp, param.active, mutaion)
					resolve("成功")
				}
			})
		})
	},
	//上拉加载更多
	reqLoadMoreList(store, param) {
		let page = Math.ceil(store.state.goodesList.length / 20 + 1)
		return new Promise((reject, resolve) => {
			uni.request({
				url: `${param.loadMoreUrl}&page=${page}`,
				method: 'GET',
				success: (resp) => {
					let mutaion = 'setGoodsLoadMore'
					goodesDataHandle(store, resp, param.active, mutaion)
					resolve("成功")
				}
			})
		})
	}
}
//商品列表数据处理函数
function goodesDataHandle(store, resp, param, mutaion) {
	let temp = resp.data.result_list.map_data
	//渲染时使用 ---------- --- --- -  -
	for (let i = 0; i < temp.length; i++) {
		temp[i].src = 'https://img.alicdn.com/bao/uploaded/i2/915497723/O1CN01Cqq4tK26vBNKEzU6M_!!0-item_pic.jpg'
		temp[i].show = true
		temp[i].loaded = false
	}
	switch (param) {
		case 0:
			store.commit(mutaion, temp)
			break
		case 1:
			let caseOne = temp.sort((n, m) => {
				if (n.coupon_amount * 1 > m.coupon_amount * 1) {
					return -1
				} else if (n.coupon_amount * 1 < m.coupon_amount * 1) {
					return 1
				} else {
					return 0
				}
			})
			store.commit(mutaion, caseOne)
			break
		case 2:
			let caseTwo = temp.sort((n, m) => {
				if (n.volume * 1 > m.volume * 1) {
					return -1
				} else if (n.volume * 1 < m.volume * 1) {
					return 1
				} else {
					return 0
				}
			})
			store.commit(mutaion, caseTwo)
			break
		case 3:
			let caseThree = temp.sort((n, m) => {
				if (n.coupon_start_fee * 1 > m.coupon_start_fee * 1) {
					return 1
				} else if (n.coupon_start_fee * 1 < m.coupon_start_fee * 1) {
					return -1
				} else {
					return 0
				}
			})
			store.commit(mutaion, caseThree)
			break
		default:
			break;
	}
}
const home_idx = {
	state,
	mutations,
	actions
}
export default home_idx
